package com.tenglong.teacher.ui.setting.setting;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.Utils;
import com.cuotibao.zhuoxiao.basics.utils.recyview.DividerSpaceItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.ClassInfo;
import com.tenglong.teacher.data.resultInfo.ClassInfoBean;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.HomePagerAdapter;
import com.tenglong.teacher.ui.common.activity.BaseActivity;
import com.tenglong.teacher.ui.homepage.PublishHomeworkActivity;
import com.tenglong.teacher.ui.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClassRecordActivity extends BaseActivity implements OnRefreshListener,HomePagerAdapter.ClassRecordListener,View.OnClickListener {

    private RecyclerView mRecyclerView;
    private HomePagerAdapter homePagerAdapter;
    private View noData;
    private SmartRefreshLayout mSmartRefreshLayout;
    private List<ClassInfoBean> seClasses;
    private ArrayList<String> stringList;
    private TextView sendWork;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_record);
        initView();
        initRecy();
        setAdapter();
        setListeners();
        showProgressDialog("数据加载中...");
        getClassInfo();
    }

    private void setListeners() {
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setOnRefreshListener(this);
        sendWork.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.sendWork:
                Intent intent = new Intent(this, PublishHomeworkActivity.class);
                intent.putExtra(PublishHomeworkActivity.KEY_IS_REISSUE, true);
                intent.putExtra(MyUtils.CLASSIDLIST, stringList);
                startActivity(intent);
                break;
        }
    }

    private void setAdapter() {
        seClasses = new ArrayList<>();
        homePagerAdapter = new HomePagerAdapter(seClasses);
        homePagerAdapter.setClassRecordListener(this);
        mRecyclerView.setAdapter(homePagerAdapter);
    }
    private void getClassInfo() {
        CreateRequestEntity.getWebService().getClassRecordByLoginName("0").enqueue(new Callback<BaseResult<List<ClassInfo>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<ClassInfo>>> call, Response<BaseResult<List<ClassInfo>>> response) {
                dismissProgressDialog();
                mSmartRefreshLayout.finishRefresh(true);
                if (ResultVerifier.isSucceed(response)) {
                    seClasses.clear();
                    seClasses.addAll(response.body().data);
                    homePagerAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
                noData.setVisibility(seClasses.size() == 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onFailure(Call<BaseResult<List<ClassInfo>>> call, Throwable t) {
                dismissProgressDialog();
                mSmartRefreshLayout.finishRefresh(false);
                ResultVerifier.systerErrers(t);
                noData.setVisibility(seClasses.size() == 0 ? View.VISIBLE : View.GONE);
            }
        });
    }

    private void initRecy() {
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerSpaceItemDecoration(Utils.dip2px(this, 15)));
    }

    private void initView() {
        setTitle("发作业");
        stringList=new ArrayList<>();
        mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setHasFixedSize(true);
        noData = findViewById(R.id.noData);
        mSmartRefreshLayout = findViewById(R.id.mSmartRefreshLayout);
        sendWork=findViewById(R.id.sendWork);
        ClassicsHeader headerView = new ClassicsHeader(this);
        ClassicsFooter footView = new ClassicsFooter(this);
        headerView.setBackgroundColor(Color.parseColor("#f8f8f8"));
        footView.setBackgroundColor(Color.parseColor("#f8f8f8"));
        mSmartRefreshLayout.setRefreshHeader(headerView);
        mSmartRefreshLayout.setRefreshFooter(footView);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getClassInfo();
    }

    @Override
    public void onRecordPosition(int position) {
        stringList.clear();
        for(ClassInfoBean classInfoBean: seClasses){
            if(classInfoBean instanceof ClassInfo && ((ClassInfo) classInfoBean).isCheck){
                stringList.add(((ClassInfo) classInfoBean).id);
            }
        }
        if(stringList.size()>0){
            sendWork.setVisibility(View.VISIBLE);
        }else{
            sendWork.setVisibility(View.GONE);
        }

    }
}
